package com.maserb.monitor.web.system_manage;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.EigenValueDictionaryEntity;
import com.maserb.monitor.enums.DataSourceEnum;
import com.maserb.monitor.enums.SampleDataCountEnum;
import com.maserb.monitor.service.system_manage.EigenValueDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/eigenValueDictionary")
public class EigenValueDictionaryController {

    @Autowired
    EigenValueDictionaryService eigenValueDictionaryService;

    @RequestMapping(value = {"/", "/index"})
    public String getIndex() {
        return "system_manage/eigenvalue_dictionary/index";
    }

    @RequestMapping("/getEditEigenValueDictionary/{id}")
    public ModelAndView getEditEigenValueDictionary(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        EigenValueDictionaryEntity entity = eigenValueDictionaryService.findOne(id);
        result.setViewName("system_manage/eigenvalue_dictionary/edit");
        result.addObject("entity", entity);
        result.addObject("dataSources", DataSourceEnum.values());
        return result;
    }

    @RequestMapping("/doUpdateEigenValueDictionary")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateEigenValueDictionary(EigenValueDictionaryEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                eigenValueDictionaryService.save(entity);
                return true;
            }
        });
    }

    @RequestMapping("/getAddEigenValueDictionary")
    public ModelAndView getAddEigenValueDictionary() {
        ModelAndView result = new ModelAndView();
        result.setViewName("system_manage/eigenvalue_dictionary/add");
        result.addObject("dataSources", DataSourceEnum.values());
        return result;
    }

    @RequestMapping("/doListEigenValueDictionary")
    @ResponseBody
    public PageResponseResult<EigenValueDictionaryEntity> doListEigenValueDictionary(PageRequestParam page, final String name) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<EigenValueDictionaryEntity>>() {
            @Override
            public Page<EigenValueDictionaryEntity> invoke(Pageable var1) throws Exception {
                Page<EigenValueDictionaryEntity> page = eigenValueDictionaryService.findAll(var1, name);
                return page;
            }
        });
    }

    @RequestMapping("/doAddEigenValueDictionary")
    @ResponseBody
    public AjaxResult<Boolean> doAddEigenValueDictionary(EigenValueDictionaryEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                eigenValueDictionaryService.save(entity);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteEigenValueDictionary")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteEigenValueDictionary(@RequestParam(value = "keys[]") ArrayList<String> keys) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                eigenValueDictionaryService.deleteAllByKeys(keys);
                return true;
            }
        });
    }
}